﻿////////////////////////////////////////////////////好友////////////////////////////////////
const config = require('../config');

module.exports = function (app, query, jwt) {
  //分享好友足迹
  app.post('/footprintshare', jwt({secret: config.secret}), function (req, res, next) {
	  console.log(req.body);
    let {id,title, brief, text } = req.body.params;
    const {userId, username} = req.user;
	const params={
		userid:userId,
		shareusername:username,
		date:new Date(),
		footprintid:id,
		footprinttitle:title,
		footprintbrief:brief,
		footprinttext:text 
	}
    query('insert into share set ? ', params, function (err, data, field) {
      if (err) {
        const json = {
          code: false,
          message: err.message
        };
        console.log(json);
        res.json(json);
      } else {
        const json = {
          code: true,
          message: "分享成功"
        };
        console.log(json);
        res.json(json);
      }
    });
  });

  //获取好友分享
  app.get('/footprintshare', jwt({secret: config.secret}), function (req, res, next) { 
    console.log("1112"); 
	const params = [
     req.user.userId,
	 req.user.userId,
	 2,
    ]; 
	query('select * from relation where (userid=? or user2id=?) and relationship=?',params, function (err, rows, field) {
   if (err) {
        const json = {
          code: false,
          message: err.message
        };
        console.log(json);
        res.json(json);
      }else{ 
          const idlist = new Set();    
        if (rows && rows.length > 0) {	 
		  for (var i = 0; i < rows.length; i++) {
            idlist.add(rows[i].userid);
            idlist.add(rows[i].user2id);
          }
          idlist.delete(req.user.userId);
		} 
		let inparams="";
		  for(item of idlist){  
			inparams+=item+",";}  	 
        let index=inparams.lastIndexOf(",");
		let inparams2=inparams.slice(0,index); 
		let sql = `select * from share where userid in (${inparams2})`;
		console.log(sql);
            query(sql, function (err, rows, field) {  
			   if (err) {
                const json = {
                  code: false,
                  message: err.message
                };
                console.log(json);
                res.json(json);
              } else {
				  console.log(rows);  	 
                let data =[];
				  for(row of rows){
				 data.push({                         
                    id: row.userid,                             
                    shareusername: row.shareusername,    
                    date: row.date, 
                    footprintid: row.footprintid,
					footprinttitle:row.footprinttitle,
					footprintbrief:row.footprintbrief,
		            footprinttext:row.footprinttext,
                  });  
				  }
		console.log(data);
		   const json = {
          code: true,
          message: "获取成功",
          data
        };
        console.log(json);
        res.json(json);
              } 
	  })	 

  }
   });
 
  });
 
  //获取好友列表 = 好友 获取
  app.get('/friendlist', jwt({secret: config.secret}), function (req, res, next) {
    const params = [
     req.user.userId,
	 req.user.userId,
	 2,
    ]; 
   query('select * from relation where (userid=? or user2id=?) and relationship=?',params, function (err, rows, field) {
   if (err) {
        const json = {
          code: false,
          message: err.message
        };
        console.log(json);
        res.json(json);
      }else{ 
        if (rows && rows.length > 0) {	  
          const idlist = new Set();    
		  for (var i = 0; i < rows.length; i++) {
            idlist.add(rows[i].userid);
            idlist.add(rows[i].user2id);
          }
          idlist.delete(req.user.userId);
		 
		let inparams="";
		  for(item of idlist){  
			inparams+=item+",";}  	 
        let index=inparams.lastIndexOf(",");
		let inparams2=inparams.slice(0,index); 
		let sql = `select * from user where userid in (${inparams2})`;
		console.log(sql);
            query(sql, function (err, rows, field) {  
			   if (err) {
                const json = {
                  code: false,
                  message: err.message
                };
                console.log(json);
                res.json(json);
              } else {
				  console.log(rows); 
        let userlist = []; 
				  for(row of rows){
				 userlist.push({                         
                    id: row.userid,   //id
                    username: row.username,    //用户名
                  }); 
				  }
		console.log(userlist);	 
        let data =userlist;
		   const json = {
          code: true,
          message: "获取成功",
          data
        };
        console.log(json);
        res.json(json);
              } 
	  })	 
	  }
	  else{
		     const json = {
              code: true,
              message: "无好友列表 ",
            };
            console.log(json);
            res.json(json);
	  } }
   });
  });

  //申请好友 = 好友 添加
  app.post('/friend', jwt({secret: config.secret}), function (req, res, next) { 
   console.log(req);
   let applyid= req.body.params.applyid;   
    query('insert into relation  values (?,?,?)', [req.user.userId,applyid,1], function (err, rows, field) {
        if (err) {
          const json = {
            code: false,
            message: err.message
          };
          console.log(json);
          res.json(json);
        } else {
         const json = {
              code: true,
              message: "申请好友成功 ",
            };
          console.log(json);
          res.json(json);
        }
      }
    );
  });

  //添加好友 = 好友 更新
  app.put('/friend', jwt({secret: config.secret}), function (req, res, next) {
   let requestid= req.body.params.requestid;  
		  console.log(requestid);
    query('select * from relation where userid=? and user2id=? and relationship=1', [requestid, req.user.userId], function (err, rows, field) {
      if (err) {
        const json = {
          code: false,
          message: err.message
        };
        console.log(json);
        res.json(json);
      } else {
		   console.log(rows);
        query('update relation  set relationship=2 where userid=? and user2id=? ', [requestid, req.user.userId], function (err, result) {
          if (err) {
            const json = {
              code: false,
              message: err.message
            };
            console.log(json);
            res.json(json);
          } else {
            const json = {
              code: true,
              message: "添加好友成功 ",
            };
            res.json(json);
          }
        });
      }
    });
  });

  //删除好友 = 好友 删除
  app.delete('/friend', jwt({secret: config.secret}), function (req, res, next) {
   let deleteuserid= req.query.deleteuserid; 
    query('delete from relation where  userid=? and user2id=? and relationship=2', [req.user.userId, deleteuserid], function (err,rows, field) {
      if (err) {
        const json = {
          code: false,
          message: err.message
        };
        console.log(json);
        res.json(json);
      } else { 
        query('delete from relation where userid=? and  user2id=? and relationship=2', [deleteuserid, req.user.userId], function (err, data, field) {
          if (err) {
            const json = {
              code: false,
              message: err.message
            };
            console.log(json);
            res.json(json);
          }
          else {
            const json = {
              code: true,
              message: "删除好友成功 ",
            };
            console.log(json);
            res.json(json);
          }
        });
      }
    });
  });
 
  //获取好友请求列表
  app.get('/friendrequestlist', jwt({secret: config.secret}), function (req, res, next) {  
    const params = [
     req.user.userId, 
	 1,
    ]; 
    query('select * from relation where user2id=? and relationship=1',params, function (err, rows, field) { 
      if (err) {
        const json = {
          code: false,
          message: err.message
        };
        console.log(json);
        res.json(json);
      }else{
        if (rows && rows.length > 0) {
		   var idlist = new Array(); 
        for (var i = 0; i < rows.length; i++) {
          idlist[i] = rows[i].userid;
        }
			let inparams="";
		  for(item of idlist){  
			inparams+=item+",";}  
        let index=inparams.lastIndexOf(",");
		let inparams2=inparams.slice(0,index); 
		let sql = `select * from user where userid in (${inparams2})`;
		console.log(sql);
            query(sql, function (err, rows, field) {  
			   if (err) {
                const json = {
                  code: false,
                  message: err.message
                };
                console.log(json);
                res.json(json);
              } else {
				  console.log(rows); 
        let userlist = []; 
				  for(row of rows){
				 userlist.push({                         
                    id: row.userid,   //id
                    username: row.username,    //用户名
                  }); 
				  }
		console.log(userlist);	 
        let data =userlist;
		   const json = {
          code: true,
          message: "获取成功",
          data
        };
        console.log(json);
        res.json(json);
              } 
	  })
	  }
	     else {
            const json = {
              code: true,
              message: "无好友请求列表 ",
            };
            console.log(json);
            res.json(json);
          }
	  }
	});
  });

  //查找用户
  app.get('/searchuser', jwt({secret: config.secret}), function (req, res, next) { 
    console.log(req); 
	let searchtext_id= req.query.searchtext_id; 
  query('select * from user where userid=?',searchtext_id, function (err, rows, field) {
      if (err) {
        const json = {
          code: false,
          message: err.message
        };
        console.log(json);
        res.json(json);
      } else {
        if (rows && rows.length > 0) {
          const data = {
            id: rows[0].userid,   //id
            username: rows[0].username,   //id
          }
          const json = {
            code: true,
            message: "查找用户成功 ",
            data
          };
        console.log(json);
          res.json(json);
        }
		else{
			 const json = {
            code: true,
            message:  "无该用户 ", 
          };
        console.log(json);
          res.json(json);
		}
      }
    });
  });
};

